<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *土地管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$manage_op=array('start','off');
if(in_array($_RQ['op'],$manage_op)){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择土地！','error');
	}
}
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','uniacid','title','status','image','remark','seed_id','classify_id','area','unarea','paixu','createtime');
	$list=pdo_getslice('jy_farmer_land',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['url']=custom_url('jy_farmer/land//id='.$item['id'],true);
			$item['classify_title']=pdo_getcolumn('jy_farmer_lclassify',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['classify_id']),'title');
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'土地列表'),
			array('op'=>'edit','title'=>'添加土地'),
		),
		'list'=>array('data'=>'list','edit'=>true,'list'=>array(
			array('name'=>'title','title'=>'土地名称','type'=>'string'),
			array('name'=>'image','title'=>'封面图','type'=>'image'),
			array('name'=>'classify_title','title'=>'地块名','type'=>'string'),
			array('name'=>'area','title'=>'面积','type'=>'string'),
			array('name'=>'status','title'=>'是否启用','type'=>'switch','set'=>'set'),
		)),
		'search'=>array(
			array('name'=>'title','width'=>15,'title'=>'土地名称','type'=>'string'),
			array('name'=>'status','width'=>15,'title'=>'是否启用','type'=>'select','options'=>'status2'),
		)
	);
	$return['htmls']=$htmls;
	$return['status2']=array(array('id'=>0,'title'=>'下架'),array('id'=>1,'title'=>'上架'));
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_farmer_land',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$item=array();
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回土地列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
						array(
							'title'=>'基本信息',
							'list'=>array(
								array('type'=>'string','title'=>'土地名称','name'=>'title','help'=>''),
								array('type'=>'image','title'=>'土地封面图','name'=>'image','help'=>''),
								array('type'=>'string','title'=>'土地总面积','name'=>'area','help'=>'仅填数字，单位:平方米'),
								array('type'=>'string','title'=>'未种植面积','name'=>'unarea','help'=>'仅填数字，单位:平方米'),
								array('name'=>'seed_id','title'=>'可种植作物','type'=>'checkbox','options'=>'seed'),
								array('name'=>'control_id','title'=>'监控设备','type'=>'select','options'=>'control'),
								array('name'=>'classify_id','title'=>'地块名称','type'=>'select','options'=>'classify','option_title'=>'title'),
								array('type'=>'string','title'=>'排序','name'=>'paixu','help'=>'填写数字，按从小到大排序展示'),
								array('type'=>'switch','title'=>'是否启用','name'=>'status','help'=>'设置是否启用'),
								array('type'=>'text','title'=>'土地简介','name'=>'remark','help'=>''),
								array('type'=>'richtext','title'=>'土地详情','name'=>'content','help'=>''),
							)
						),
			
			)
	);
	$return['seed']=pdo_getall('jy_farmer_seed',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	foreach($return['seed'] as &$v){
		$v=$v['title'];
	}
	if($this->plugin_exists('jy_live')){
		$return['control']=pdo_getall('jy_live_list',array('uniacid'=>$_SESSION['uniacid']),array('id','title'));
	}
	$return['classify']=pdo_getall('jy_farmer_lclassify',array('uniacid'=>$_SESSION['uniacid']),array('id','title'));
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array(
		'title'=>trim($postdata['title']),
		'area'=>intval($postdata['area']),
		'unarea'=>intval($postdata['unarea']),
		'image'=>trim($postdata['image']),
		'seed_id'=>trim($postdata['seed_id']),
		'control_id'=>intval($postdata['control_id']),
		'classify_id'=>intval($postdata['classify_id']),
		'remark'=>trim($postdata['remark']),
		'content'=>trim($postdata['content']),
		'status'=>intval($postdata['status']),
		'paixu'=>intval($postdata['paixu']),
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_farmer_land',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		pdo_insert('jy_farmer_land',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_farmer_land',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要编辑的土地！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_farmer_land',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
